package ceet.ac.cn.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ceet.ac.cn.dao.AdminDao;
import ceet.ac.cn.model.Admin;

/**
 * Servlet implementation class UpdateServlet
 */
@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String idStr = req.getParameter("id");
        if(idStr != null && !idStr.equals("")){
            int id = Integer.valueOf(idStr);
            AdminDao dao = new AdminDao();
            Admin admin = dao.selectAdminById(id);
            req.setAttribute("admin", admin);
        }
        req.getRequestDispatcher("update.jsp").forward(req, resp);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String username = req.getParameter("username");
        String userpwd = req.getParameter("userpwd");
        String idStr = req.getParameter("id");
        Admin admin = new Admin();
        admin.setId(Integer.valueOf(idStr));
        admin.setUsername(new String(username.getBytes("ISO-8859-1"),"UTF-8"));
        admin.setUserpwd(new String(userpwd.getBytes("ISO-8859-1"),"UTF-8"));
        AdminDao dao = new AdminDao();
        dao.updateAdmin(admin);
        req.getRequestDispatcher("ShowServlet").forward(req, resp);
	}

}
